package com.ota.client.tools;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class JWT {

    public static String HEAD_256 = "{\"alg\":\"HS256\"}";
    public static String HEAD_512 = "{\"alg\":\"HS512\"}";
    public static int HS256 = 0;
    public static int HS512 = 1;
    public static Hash[] hashs={
        Hash.SHA256,
        Hash.SHA512
    };
    public static String[] heads={
       URL64.encode(HEAD_256.getBytes()).replaceAll("=", ""),
        URL64.encode(HEAD_512.getBytes()).replaceAll("=", ""),
    };


     int alg=0;
    public JWT hs256(){
        alg=0;
        return this;
    }
    public JWT hs512(){
        alg=1;
        return this;
    }


    private String key="";
    public JWT key(String key){
        this.key = key;
        return this;
    }
    private Map payload = new HashMap();
    public JWT payload(String k,Object v){
        payload.put(k, v);
        return this;
    }
    
    public String genJwt(){
        String head=heads[alg];
        payload.put("iat", System.currentTimeMillis()/1000);
        payload.put("jti", UUID.randomUUID().toString().replaceAll("-", ""));
        String pl=URL64.encode(JsonString.asJsonString(payload).getBytes()).replaceAll("=", "");
        return getjwt(join(head,".",pl), key);
    }
    

    protected String getjwt(String jwt,String key){
        Hash hash =getHash();
        HMAC hmac = new HMAC(key.getBytes(),hash);
        byte[] b= hmac.sum(jwt.getBytes());
        return join(jwt,".",URL64.encode(b).replaceAll("=", ""));
    }

    protected Hash getHash(){
       return hashs[alg];
    }

    protected String join(String ...str){
        StringBuilder sb = new StringBuilder();
        for(String s:str){
            sb.append(s);
        }
        return sb.toString();
    }

    public boolean check(String jwt){
        String[] jwts = jwt.split("\\.");
        if(jwts.length!=3)return false;
        String headString = jwts[0];
        String payloadString = jwts[1];
        String sign = jwts[2];
        HMAC hmac = new HMAC(key.getBytes(),getHash());
        byte[] b= hmac.sum(join(headString,".",payloadString).getBytes());
        return sign.equals(URL64.encode(b));
    }
    public String info(String jwt){
         String[] jwts = jwt.split("\\.");
        if(jwts.length!=3)return "";
        String headString = jwts[0];
        String payloadString = jwts[1];
        return join("header:",
        new String(URL64.decode(headString)),
        "\r\npaload:",new String(URL64.decode(payloadString)),
        "\r\npass:",String.valueOf(check(jwt))
        );
    }

    public static void main(String[] args) {
        JWT jwt = new JWT();
        jwt.hs512().key("123456");
        jwt.payload("name", "zhangsan");
        jwt.payload("age", 18);
        jwt.payload("sex", "male");
        jwt.payload("birthday", "1990-01-01");
        jwt.payload("address", "中国");
        jwt.payload("phone", "1234567890");
        System.out.println(jwt.genJwt());
        System.out.println(jwt.check(jwt.genJwt()));
        System.out.println(jwt.info(jwt.genJwt()));
    }
}
